package dao.userDao;

import cn.hutool.core.lang.Validator;
import dao.BaseDao;
import pojo.User;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UserDao extends BaseDao {

    /**
     * 根据UserCode查询用户
     *
     * @param id
     * @return
     */
    public User getUserByUserCode(String id) {
        Validator.validateNotEmpty(id, "唯一标识不能为空");

        String sql = "select * from user where user_code = ? or id=?";
        Object[] params = {id, id};
        rs = query(sql, params);
        User user = null;
        try {
            if (rs.next()) {
                user = new User();
                user.setId(rs.getString("id"));
                user.setUserCode(rs.getString(2));
                user.setPasswrod(rs.getString(3));
                user.setUserName(rs.getString(4));
                user.setSex(rs.getString(5));
                user.setRole(rs.getInt(6));
                user.setMoney(rs.getDouble(7));
                user.setPhone(rs.getString(8));
                user.setBirth(rs.getDate(9));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            //关闭资源
            closeAll(conn, pstm, rs);
        }
        return user;
    }

    /**
     * 添加用户
     *
     * @param user 参数
     * @return
     */
    public int addUser(User user) {
        //TODO 校验
        Validator.validateNotEmpty(user.getPhone(), "手机不能为空");
        Validator.validateNotEmpty(user.getId(), "ID不能为空");
        Validator.validateNotEmpty(user.getUserCode(), "用户账号不能为空");
        Validator.validateNotEmpty(user.getPasswrod(), "用户密码不能为空");
        Validator.validateNotEmpty(user.getUserName(), "用户名称不能为空");
        Validator.validateNotEmpty(user.getSex(), "性别不能为空");
        Validator.validateNotEmpty(user.getRole(), "商家不能为空");
        Validator.validateNotEmpty(user.getMoney(), "资金不能为空");
        Validator.validateNotEmpty(user.getBirth(), "出生日期不能为空");
        String sql = "insert into user (`id`,`user_code`,`passwrod`,`user_name`,`sex`,`role`,`money`,`phone`,`birth`) values(?,?,?,?,?,?,?,?,?)";
        Object[] params = {user.getId(), user.getUserCode(), user.getPasswrod(), user.getUserName(), user.getSex(), user.getRole(), user.getMoney(), user.getPhone(), user.getBirth()};


        int rows = execute(sql, params);
        //关闭资源
        closeAll(conn, pstm, rs);
        return rows;
    }

    /**
     * 修改用户信息
     *
     * @param user
     * @return
     */
    public int updateUser(User user) {
        Validator.validateNotEmpty(user.getPhone(), "手机不能为空");
        Validator.validateNotEmpty(user.getUserName(), "用户名称不能为空");
        Validator.validateNotEmpty(user.getSex(), "性别不能为空");
        Validator.validateNotEmpty(user.getRole(), "商家不能为空");
        Validator.validateNotEmpty(user.getId(), "用户Id不能为空");
        Validator.validateNotEmpty(user.getBirth(), "生日不能为空");

        String sql = "update `user` set `user_name`=?,`sex`=?,`role`=?,`phone`=?,`birth`=? where id=?";
        Object[] params = {user.getUserName(), user.getSex(), user.getRole(), user.getPhone(), user.getBirth(), user.getId()};
        int rows = execute(sql, params);
        closeAll(conn, pstm, rs);
        return rows;
    }

    /**
     * 删除用户
     *
     * @param id
     * @return
     */
    public int deleteUser(String id) {
        Validator.validateNotEmpty(id, "唯一标示不能为空");

        String sql = "DELETE FROM user where id=? or user_code=?";
        Object[] params = {id, id};

        int rows = execute(sql, params);
        closeAll(conn, pstm, rs);
        return rows;
    }

    /**
     * 查询所有用户信息
     *
     * @return
     */
    public List<User> getUserList() {
        List<User> userList = new ArrayList<>();
        String sql = "select * from `user`";
        rs = query(sql, null);


        try {
            while (rs.next()) {
                User user=new User();
                user.setId(rs.getString(1));
                user.setUserCode(rs.getString(2));
                user.setPasswrod(rs.getString(3));
                user.setUserName(rs.getString(4));
                user.setSex(rs.getString(5));
                user.setRole(rs.getInt(6));
                user.setMoney(rs.getDouble(7));
                user.setBirth(rs.getDate(9));
                user.setPhone(rs.getString(8));
                userList.add(user);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            closeAll(conn,pstm,rs);
        }

        return userList;
    }

}
